<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="webjars/jquery/3.6.4/jquery.min.js"></script>
    <link href="webjars/bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <script>
        $(function (){
            $(".btn-primary").click(function (){
                location="update?uid="+$(this).val();
            })
            $(".btn-warning").click(function (){
                $.get(
                    "status/"+$(this).val(),
                     function (obj){
                        if(obj) {
                            alert("修改状态成功！");
                            location.reload()
                        }else{
                              alert("失败！");
                        }

                      }

                )
            })

            //删除
            $(".btn-warning").click(function (){
                //判断是否选择
                var $name = $("[name='del']:checked");
                if($name.length==0){
                    alert("对不起，请选择！")
                }else{
                    //获取要删除的uid
                    let values="";
                    $name.each(function (){
                        values += $(this).val()+','
                    })
                    if(confirm("?")){
                           $.get(
                               "del",
                               {ids:values},
                               function (data){
                                   if(data){
                                       alert("删除成功！");
                                       location.reload()
                                   }else{
                                       alert("对不起，失败！")
                                   }
                               }
                           )
                    }
                }

            })
            //全选
            $("#all").click(function (){
                $("[name='del']").prop("checked",$(this).prop("checked"));
            })
        })
    </script>
</head>
<body>

      <div class="container">
           <h4>欢迎登录[[${session.logUser.realname}]]，<a th:href="@{logout}">注销</a></h4>
           <table class="table table-bordered table-hover">
               <tr>
                   <td>
                       <input type="checkbox" id="all"> <button class="btn btn-warning">删除</button>
                   </td>
                 <th>编号</th>
                 <th>账号</th>
                 <th>密码</th>
                 <th>姓名</th>
                 <th>性别</th>
                 <th>生日</th>
                 <th>状态</th>
                 <th>角色</th>
                   <th>
                       <button class="btn btn-success" onclick="location='add'">新增</button>
                   </th>
               </tr>

             <tr th:each="user:${pager.list}" th:object="${user}">
                 <td><input type="checkbox" name="del" th:value="*{uid}"></td>
                 <td th:text="*{uid}"></td>
                 <td th:text="*{username}"></td>
                 <td th:text="*{password}"></td>
                 <td th:text="*{realname}"></td>
                 <td th:text="*{sex} == 0 ? '男': '女'"></td>
                 <td th:text="*{#dates.format(birthday,'yyyy-MM-dd')}"></td>
                 <td th:text="*{status} ==0  ? '活动' : '禁用'"></td>
                 <td>
                     <span th:each="role:*{roles}" th:object="${role}">
                       <span th:text="*{rname}"></span>
                     </span>
                 </td>
                 <td>
                     <button th:value="*{uid}" class="btn btn-primary">修改</button>
                     <button class="btn btn-warning" th:value="*{uid}" th:text="*{status}==0 ? '禁用' : '活动'"></button>
                 </td>
             </tr>
               <tr>
                   <td colspan="10" align="center">
                       共<span th:text="${pager.total}"></span>条数据，共<span th:text="${pager.pages}"></span>页，
                       当前是第<span th:text="${pager.pageNum}"></span>页。
                       <a th:href="@{list(offset=1,username=${username},realname=${realname})}">首页</a>
                       <a th:if="${pager.hasPreviousPage}" th:href="@{list(offset=${pager.prePage},username=${username},realname=${realname})}">上页</a>
                       <a th:if="${pager.hasNextPage}" th:href="@{list(offset=${pager.nextPage},username=${username},realname=${realname})}">下页</a>
                       <a th:href="@{list(offset=${pager.pages},username=${username},realname=${realname})}">末页</a>
                   </td>
               </tr>
           </table>
     </div>

</body>
</html>
